﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Collections;
using System.IO;

namespace UFSSWebApp
{
    public partial class userfile : System.Web.UI.Page
    {
        UFSSservicereference.UserCreds uc = new UFSSservicereference.UserCreds();
        UFSSservicereference.UFSSserviceSoapClient ussc = new UFSSservicereference.UFSSserviceSoapClient();
        private UFSSservicereference.UFSSFile file;
        private ArrayList files = new ArrayList();

        protected void Page_Load(object sender, EventArgs e)
        {
            uc = (UFSSservicereference.UserCreds)Session["usercreds"];
            if (Request.QueryString["id"] == null)
            {
                file = (UFSSservicereference.UFSSFile)Session["fileinfo"];
            }
            else
            {
                try
                {
                    file = ussc.getFileInfo(uc,Int32.Parse(Request.QueryString["id"]));
                    Session.Remove("fileinfo");
                    Session.Add("fileinfo", file);
                }
                catch
                {
                    file=null;
                }
            }
            if (!IsPostBack && file!=null)
                refresh();
        }

        private void refresh()
        {
            lblDLCount.Text = file.downloads.ToString();
            lblFileSize.Text = Math.Ceiling((double)(file.filesize / 1024)).ToString() + "Kb";
            lblUploaded.Text = file.dateadded.ToString();
        }

        protected void lbtnOwner_Click(object sender, EventArgs e)
        {
            Session.Remove("user");
            Session.Add("user", ussc.getUserName(file.ownerid));
            Response.Redirect("~/userpage.aspx");
        }

        protected void lbtnSub_Click(object sender, EventArgs e)
        {
            Session.Remove("sub");
            Session.Add("sub", file.subname);
            Response.Redirect("~/usersubs.aspx");
        }

        protected void btnDownload_Click(object sender, EventArgs e)
        {
            //FileReceiver fr = new FileReceiver();
            //fr.retrieveFile(uc, file.userid);
            //HttpResponse resp = fr.retrieveFile(uc, file.userid);
            bool success = retrieveFile(uc, file.userid);
            if (success == false)
                Response.Redirect("Error.aspx");
        }

        public bool retrieveFile(UFSSservicereference.UserCreds uc, int fileid)
        {
            try
            {
                UFSSservicereference.UFSSFile fileinfo;
                byte[] file = null;

                fileinfo = ussc.getFileInfo(uc, fileid);
                ussc.DownloadFile(uc, fileid, ref file);
                FileReceiver fr = new FileReceiver(fileinfo.filename, file);
                string path = fileinfo.hash + "\\" + fileinfo.filename;
                fr.WriteFile(path);
                Response.Buffer = true;
                Response.Clear();
                Response.ClearContent();
                Response.ClearHeaders();
                Response.ContentType = "application/octet-stream";
                Response.AppendHeader("Content-Disposition", "attachment; filename=" + fileinfo.filename);
                string mappath = Server.MapPath(path);
                Response.TransmitFile(Directory.GetCurrentDirectory() + "\\" + path);
                Response.End();
                return true;
            }
            catch
            {
                return false;
            }
        }
    }
}